package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.naming.java.javaURLContextFactory;

import pojo.User;
import util.DBUtil;
import dao.UserDao;

public class UserDaoImpl implements UserDao {

	@Override
	public int addUser(User user) {
		String sql="insert into users(id,uname,sal,birth,sex) values (seq01.nextval,?,?,?,?)";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, user.getName());
			pst.setDouble(2, user.getSal());
			pst.setDate(3, new java.sql.Date(user.getBirth().getTime()));
			pst.setBoolean(4, user.isSex());
			m=pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally{
			DBUtil.close(null, pst, null);
		}
	}

	@Override
	public int deleteUser(Integer id) {
		String sql="delete from users where id=?";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setInt(1, id);
			m=pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally{
			DBUtil.close(null, pst, null);
		}
	}

	@Override
	public int updateUser(User user) {
		String sql="update users set uname=?,sal=?,birth=?,sex=? where id=?";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, user.getName());
			pst.setDouble(2, user.getSal());
			pst.setDate(3, new java.sql.Date(user.getBirth().getTime()));
			pst.setBoolean(4, user.isSex());
			pst.setInt(5, user.getId());
			m=pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally{
			DBUtil.close(null, pst, null);
		}
	}

	@Override
	public User loadUser(Integer id) {
		String sql="select * from users where id=?";
		Connection connection = null;
		PreparedStatement pst=null;
		User user = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setInt(1, id);
			ResultSet rs = null;
			rs=pst.executeQuery();
			while(rs.next()){
				user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("uname"));
				user.setSal(rs.getDouble("sal"));
				user.setBirth(rs.getDate("birth"));
				user.setSex(rs.getBoolean("sex"));
			}
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			DBUtil.close(connection, pst, null);
		}
	}

	@Override
	public List<User> findAll() {
		List<User> list = new ArrayList<User>();
		
		String sql="select * from users";
		Connection connection = null;
		PreparedStatement pst=null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			ResultSet rs = null;
			rs=pst.executeQuery();
			
			while(rs.next()){
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("uname"));
				user.setSal(rs.getDouble("sal"));
				user.setBirth(rs.getDate("birth"));
				user.setSex(rs.getBoolean("sex"));
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		} finally{
			DBUtil.close(connection, pst, null);
		}
	}

}
